"""
考虑:评论与子评论
"""
from flask import Flask
from flask_sqlalchemy import SQLAlchemy

class Config(object):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = 'mysql://root:mysql@localhost:3306/selfManyToMany'
    SQLALCHEMY_TRACK_MODIFICATIONS = True
    SECRET_KEY = 'asfoiajsifjafjapons'


app = Flask(__name__)
app.config.from_object(Config)

db = SQLAlchemy(app)

class Comment(db.Model):
    __tablename__ = "comments"
    id = db.Column(db.Integer, primary_key=True)
    # nullable=False 表示name字段不可为null
    content = db.Column(db.String(128),unique=True,nullable=False)
    # 父评论ID
    parent_id = db.Column(db.Integer,db.ForeignKey('comments.id'))
    # 父评论模型  remote_side=[id] 表示关联的是谁
    """
    :param remote_side:
          used for self-referential relationships, indicates the column or
          list of columns that form the "remote side" of the relationship.
    """
    parent = db.relationship('Comment',remote_side=[id],
                             backref=db.backref('childs',lazy='dynamic'))


@app.route('/')
def index():
    return 'hello world'

if __name__ == '__main__':
    db.drop_all()
    db.create_all()

    com1 = Comment(content='我是主评论1')
    com2 = Comment(content='我是主评论2')
    com11 = Comment(content='我是回复主评论1的子评论1')
    com11.parent = com1
    com12 = Comment(content='我是回复主评论1的子评论2')
    com12.parent = com1

    db.session.add_all([com1, com2, com11, com12])
    db.session.commit()
    app.run(debug=True)


    app.run()